<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>网站协议接受验证</title>
</head>
<body>
  <form action="https://www.baidu.com" id="form">
    <label for="">用户名</label>
    <input type="text" name="user">
    <hr>
    <input type="checkbox" name="copyright"><span>接受协议</span>
    <hr>
    <button>提交</button>
  </form>
  

  <script>
    // 封装查询
    function query(el){
      return document.querySelector(el);
    }
    query('#form').addEventListener('submit',(event)=>{
      let user = query("[name='user']").value.trim();
      let copyright = query("[name='copyright']").checked;
      console.log(user.length)
      console.log(copyright)
      // event.preventDefault();
      if(!user || copyright  === false){
        event.preventDefault();//阻止默认 提交是事件
        alert("请解救协议并且添加用户名")
      }
    })

  </script>
</body>
</html>